#! -*- coding: utf-8 -*-
"""
@Author: Gump
@Create Time: 20241107
@Info: 小车运动控制客户端
"""

import rclpy
from rclpy.node import Node
from rclpy.action import ActionClient

class MoveControlClient(Node):
    """运动控制客户端"""
    def __init__(self, name):
        super().__init__(name)
        self.action_client = ActionClient(
            self,
            srv,  # 接口类型
            'move_control'  # 动作名称
        )

    def send_goal(self, enable):
        """发送动作目标"""
        goal_msg = None  # 动作目标消息
        self.action_client.wait_for_server()  # 等待动作的服务端启动
        self.send_goal_future = self.action_client.send_goal_async(
            goal_msg,  # 动作目标
            feedback_callback=self.feedback_callback  # 反馈消息的回调函数
        )

        self.send_goal_future.add_done_callback(self.goal_response_callback)  # 服务端收到目标之后反馈的回调函数

    def goal_response_callback(self, future):
        """服务端收到目标之后反馈消息的回调函数"""
        goal_handle = future.result()  # 接收动作的结果
        if not goal_handle.accepted:
            self.get_logger().info('Goal rejected :(')
            return
        
        self.get_logger().info('Goal accepted :)')
    
    def get_result_callback(self, future):
        """收到最终结果的回调函数"""
        result = future.result().result
        self.get_logger().info('Result: {%d}' % result.finish)

    def feedback_callback(self, feedback_msg):
        """处理周期反馈消息的回调函数"""
        feedback = feedback_msg.feedback
        self.get_logger().info('Received feedback: {%d}' % feedback.state)


def main(args=None):
    rclpy.init(args=args)
    node = MoveControlClient('action_move_client')
    node.send_goal(True)
    rclpy.spin(node)
    node.destroy_node()
    rclpy.shutdown()